home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 420 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.4 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: tony@online.tmx.com.au (Tony Cook)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Article for comp.std.c++: Eliminating #ifdef: if const
  5. Date: 20 Feb 1996 10:48:56 PST
  6. Organization: Home
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <199602190008.LAA23411@online.tmx.com.au>
  9. References: <199602160807.IAA06126@condor.ukc.ac.uk>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Mon, 19 Feb 1996 11:08:06 +1100
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMSoXpky4NqrwXLNJAQE4DwH+Lct7p4PNDix8hhGra5dehRyQZV3Z+Onh
  15.     rhNmgBmHyGiHSc7K0zN1P9YtiTIh8pybI55LlWCMYJNIYeVMVEZ7/Q==
  16.     =BrSV
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Mark Russell (M.T.Russell@ukc.ac.uk) wrote:
  20. : Subject: Eliminating #ifdef: if const
  21.  
  22. : Here's an idea I had for a non-preprocessor replacement for #if and  #ifdef.
  23.  
  24. :     if const (<expr>) { <stuff> }
  25.  
  26. : `if const' is a compile time version of `if'.  <expr> is a constant
  27. : expression which is convertible to bool.  If it yields true, <stuff>
  28. : is processed as normal.  Otherwise <stuff> is parsed into tokens, but
  29. : ignored except for counting { and } nesting.  <stuff> is anything that
  30. : can appear inside a namespace block.  `if const' does not introduce a
  31. : new scope.
  32.  
  33. : The following is also legal, with the obvious meaning:
  34.  
  35. :     if const (<expr>) { <stuff> } else const { <other stuff> }
  36.  
  37. : There are also a number of compile-time pseudo-functions (I hesitate
  38. : to call them pragmas):
  39.  
  40. :     bool #os_version(const char* str)
  41.  
  42. :         os version (uname -r) at or later than str
  43.  
  44. :     bool #os_name(const char *str)
  45.  
  46. :         os name (uname -s) is str
  47.  
  48. These would require mechanisms to supply operating system parameters
  49. to the compiler - besides, a module compiled for one operating
  50. system can sometimes be linked into an executable for another on
  51. (some 16-bit DOS modules can be used in a Windows program).
  52.  
  53. OS/2 FAPI programs can run under DOS, OS/2 or Windows NT.
  54.  
  55. :     bool #compiler_name(const char *)
  56. :     bool #compiler_version(const char*)
  57.  
  58. :         Same tests for the compiler
  59.  
  60. These might be nice.
  61.  
  62. : Using these you could do things like this:
  63.  
  64. :     // Are we running IRIX 5.3
  65. :     const bool using_irix_53 = #os_name("IRIX") &&
  66. :                    #os_version("5.3") && !#os_version("5.4")
  67.  
  68. :     if const (using_irix_53) {
  69. :         // Do some stuff needed just for IRIX 5.3
  70. :     }
  71.  
  72. :     if const (#compiler("g++")) {
  73. :         // Do some g++ stuff
  74. :     }
  75.  
  76. :     if const (#header_exists("sys/frobnitz9000.h")) {
  77. :         #include "sys/frobnitz9000.h"
  78. :         // Do stuff for the Frobnitz 9000
  79. :     }
  80.  
  81. Doesn't this bind the preprocess and compiler closer together?  For
  82. the compilation stages in the WP won't the #include occur in stage 4
  83. and the if const in stage 7?
  84.  
  85. : With this I could personally stop using the preprocessor for anything
  86. : except #include.  If `include' (as described in D&E) is also
  87. : implemented, I could stop using the preprocessor altogether, which
  88. : would be nice.
  89.  
  90. What's the difference between what you've specified and the
  91. preprocessor (other than different syntax?)
  92.  
  93. -- 
  94.         Tony Cook - tony@online.tmx.com.au
  95.                     100237.3425@compuserve.com
  96. ---
  97. [ To submit articles: Try just posting with your newsreader.  If that fails,
  98.                       use mailto:std-c++@ncar.ucar.edu
  99.   FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  100.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  101.   Comments? mailto:std.c++-request@ncar.ucar.edu 
  102. ]
  103.